﻿Imports Telerik.Web.UI
Imports System.Data.SqlClient
Imports System.Data

Partial Class BudgetTransferDtl
    Inherits System.Web.UI.Page

    Dim _CommandName As String
    Dim _Command As String
    Dim Grid As RadGrid
    Dim BudgetTransferHdrObj As New DBBudgetTransferHdrSP
    Dim BudgetTransferHdrDS As New Data.DataSet
    Dim BudgetTransferDtlObj As New DBBudgetTransferDtlSP
    Dim BudgetTransferDtlDS As New Data.DataSet

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")

            hdnBTID.Value = Request.QueryString("ID").ToString

            If hdnBTID.Value <> "" And hdnBTID.Value <> "0" And hdnBTID.Value <> "-1" Then
                FillBudgetTransferDtl()
            Else
                hdnBTID.Value = "-1"
                rdpBTDate.DbSelectedDate = DateTime.Now()
                rbtnDelete.Visible = False
            End If
            btnCancel.Attributes.Add("onClick", "javascript:history.back(); return false;")
        End If
    End Sub


    Protected Sub rgBudgetTransferDtl_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgBudgetTransferDtl.ItemCommand
        _CommandName = e.CommandName
    End Sub


    Sub SaveControls()
        hdnBTID.Value = BudgetTransferHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteScalar, "SaveBudgetTransferHdr", _
                                                      hdnBTID.Value, rtbBTNo.Text, rntbYear.Text, rdpBTDate.DbSelectedDate, rtbRemarks.Text, , _
                                                      Session("UserName").ToString(), Now)
        SaveDtl()
    End Sub

    Protected Sub SaveDtl()

        BudgetTransferDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteBudgetTransferDtlByBTID", , hdnBTID.Value)

        For counter = 0 To rgBudgetTransferDtl.Items.Count - 1
            BudgetTransferDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "SaveBudgetTransferDtl", rgBudgetTransferDtl.Items(counter)("ID").Text, _
            hdnBTID.Value, _
            IIf(rgBudgetTransferDtl.Items(counter)("FromProgram").Text = "&nbsp;", "", rgBudgetTransferDtl.Items(counter)("FromProgram").Text), _
            IIf(rgBudgetTransferDtl.Items(counter)("FromAccount").Text = "&nbsp;", "", rgBudgetTransferDtl.Items(counter)("FromAccount").Text), _
             IIf(rgBudgetTransferDtl.Items(counter)("ToProgram").Text = "&nbsp;", "", rgBudgetTransferDtl.Items(counter)("ToProgram").Text), _
             IIf(rgBudgetTransferDtl.Items(counter)("ToAccount").Text = "&nbsp;", "", rgBudgetTransferDtl.Items(counter)("ToAccount").Text), _
             IIf(rgBudgetTransferDtl.Items(counter)("Amount").Text = "&nbsp;", "", rgBudgetTransferDtl.Items(counter)("Amount").Text))
        Next
    End Sub

    Protected Sub FillBudgetTransferDtl()
        BudgetTransferHdrDS = BudgetTransferHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedBudgetTransferHdr", hdnBTID.Value)
        If Not BudgetTransferHdrDS Is Nothing Then
            If BudgetTransferHdrDS.Tables.Count > 0 Then
                If BudgetTransferHdrDS.Tables(0).Rows.Count > 0 Then

                    rntbYear.Text = BudgetTransferHdrDS.Tables(0).Rows(0).Item("BTYear").ToString()
                    rtbBTNo.Text = BudgetTransferHdrDS.Tables(0).Rows(0).Item("BTNo").ToString()
                    rdpBTDate.DbSelectedDate = BudgetTransferHdrDS.Tables(0).Rows(0).Item("BTDate").ToString()
                    rtbRemarks.Text = BudgetTransferHdrDS.Tables(0).Rows(0).Item("Remarks").ToString()

                    'If hdnRecordStatus.Value = "2" Then 'Voucher is exported so fields disabled
                    '    rtbYear.Enabled = False
                    'End If
                End If
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            SaveControls()
            RadAjaxManager1.ResponseScripts.Add("radalert('Information Saved Successfully !', 270, 100);")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    'Protected Overrides Sub RaisePostBackEvent(ByVal source As IPostBackEventHandler, ByVal eventArgument As [String])
    '    If source Is rbtnDelete.ClientID Then
    '        hdnReason.Value = eventArgument.ToString()
    '        MyBase.RaisePostBackEvent(source, eventArgument)
    '    End If
    'End Sub

    Protected Sub rbtnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbtnDelete.Click
        DeleteRecord()
    End Sub

    Sub DeleteRecord()
        Try
            'BudgetTransferDtlObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteBudgetTransferDtlByBTID", , hdnBTID.Value)
            BudgetTransferHdrObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.ExecuteNonQuery, "DeleteBudgetTransferHdr", hdnBTID.Value, , , , , , , , hdnReason.Value)
            Response.Redirect("~/BudgetTransferHdr.aspx")

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally

        End Try
    End Sub

    Protected Sub rgBudgetTransferDtl_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgBudgetTransferDtl.ItemDataBound
        If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) And _CommandName <> "InitInsert" Then
            'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then

            Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
            Dim parentItem As GridDataItem = editFormItem.ParentItem

            Dim box As RadComboBox = CType(editFormItem.FindControl("WUCFromProgram").FindControl("rcbv"), RadComboBox)
            box.SelectedValue = parentItem("FromProgram").Text
            Dim box1 As RadComboBox = CType(editFormItem.FindControl("WUCFromProgram").FindControl("rcbd"), RadComboBox)
            box1.SelectedValue = parentItem("FromProgram").Text

            Dim box2 As RadComboBox = CType(editFormItem.FindControl("WUCToProgram").FindControl("rcbv"), RadComboBox)
            box2.SelectedValue = parentItem("ToProgram").Text
            Dim box3 As RadComboBox = CType(editFormItem.FindControl("WUCToProgram").FindControl("rcbd"), RadComboBox)
            box3.SelectedValue = parentItem("ToProgram").Text

            Dim box4 As RadComboBox = CType(editFormItem.FindControl("WUCFromAccount").FindControl("rcbv"), RadComboBox)
            box4.SelectedValue = parentItem("FromAccount").Text
            Dim box5 As RadComboBox = CType(editFormItem.FindControl("WUCFromAccount").FindControl("rcbd"), RadComboBox)
            box5.SelectedValue = parentItem("FromAccount").Text

            Dim box6 As RadComboBox = CType(editFormItem.FindControl("WUCToAccount").FindControl("rcbv"), RadComboBox)
            box6.SelectedValue = parentItem("ToAccount").Text
            Dim box7 As RadComboBox = CType(editFormItem.FindControl("WUCToAccount").FindControl("rcbd"), RadComboBox)
            box7.SelectedValue = parentItem("ToAccount").Text

        End If

    End Sub


End Class
